//
//  FulisheShopController.m
//  ToListenPhone
//
//  Created by zhibao on 2023/7/21.
//

#import "FLSShopController.h"
#import "FLSShopFirstReusableView.h"
#import "FLSShopCell.h"
@interface FLSShopController ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    
    CGFloat _itemWH;
    CGFloat _margin;
}
@property (nonatomic, strong) MYCollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation FLSShopController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_statusBarStyle = UIStatusBarStyleDarkContent;
    self.gk_backImage = [UIImage imageNamed:@"arrow_back"];
    UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
    [appearance configureWithOpaqueBackground];
    appearance.backgroundColor = UIColor.clearColor;
    //去掉导航栏下的阴影线
    appearance.shadowColor = UIColor.clearColor;
    self.gk_navigationBar.standardAppearance = appearance;
    self.gk_navigationBar.scrollEdgeAppearance = self.gk_navigationBar.standardAppearance;
    [self configCollectionView];
    _dataArray = [NSMutableArray new];
    __weak __typeof(self)weakSelf = self;
    [[HttpHelper sharedInstance] sendGet:@"api/integral/product" parameters:@{} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.dataArray = responseObject.data[@"data"];
        [strongSelf.collectionView reloadData];
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
}
- (void)configCollectionView {
    // 如不需要长按排序效果，将LxGridViewFlowLayout类改成UICollectionViewFlowLayout即可
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[MYCollectionView alloc] initWithFrame:CGRectMake(0, 0, MY_Screen_W, MY_Screen_H) collectionViewLayout:_layout];
    _collectionView.alwaysBounceVertical = YES;
    _collectionView.backgroundColor = MyColor(@"#F6F6F6");
    _collectionView.contentInset = UIEdgeInsetsMake(0.1, 0, 0, 0);
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.view addSubview:_collectionView];
    [_collectionView setNoDataType:TableViewNoDataType_FILE];
    [_collectionView.mj_footer endRefreshingWithNoMoreData];
    [_collectionView registerClass:[FLSShopCell class] forCellWithReuseIdentifier:@"FLSShopCell"];
    [_collectionView registerClass:[FLSShopFirstReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FLSShopFirstReusableView"];
    [_collectionView reloadData];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _margin = 15;
    _itemWH = (MY_Screen_W  - 2)/ 2;
    _layout.itemSize = CGSizeMake(_itemWH, 229);
    _layout.minimumInteritemSpacing = 0;
    _layout.minimumLineSpacing = 0;
    [self.collectionView setCollectionViewLayout:_layout];
}

#pragma mark UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        FLSShopFirstReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FLSShopFirstReusableView" forIndexPath:indexPath];
        [headView update:[NSString stringWithFormat:@"%@",[UserInfoManager sharedManager].integral]];
           return headView;
       
    } else {
        return nil;
    }
}
//头部视图frame
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(kScreenWidth, pixw(215) + 20);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = _dataArray[indexPath.row];
    FLSShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FLSShopCell" forIndexPath:indexPath];
    [cell update:dic];
    __weak typeof(self) _self = self;
    cell.duihuanBlock = ^{
        __strong typeof(_self) self = _self;
        [[HttpHelper sharedInstance] sendPost:@"api/integral/product/pay" parameters:@{@"product_id":dic[@"product_id"]} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
            if(responseObject.code == 1){
                [QMUITips showSucceed:@"兑换成功！"];
                [[UserInfoManager sharedManager] getUserInfoData:^(BOOL flag) {
                    [self.collectionView reloadData];
                }];
            }else{
                [QMUITips showError:@"您的积分不足！"];
            }
        } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
            
        }];
        DLog(@"兑换");
    };
    return cell;
}


#pragma mark - Click Event


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}
@end
